package com.ocean.common.utils;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.net.URLEncoder;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletResponse;

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;


public class CreateWordUtils {
	private Configuration configuration = null;

    public CreateWordUtils(){
        configuration = new Configuration();
        configuration.setDefaultEncoding("UTF-8");
    }

   /**参数说明：
    * templatePath: 模版路径。generateFilePath：生成文件的路径。templateName：模版名称。dataMap：需要替换的数据
    * 返回值为：生成文件存放的文件夹的路径。
    */
    public String createWord(String templatePath, String generateFilePath,String templateName,Map<String,Object> dataMap){
        try {
			configuration.setDirectoryForTemplateLoading(new File(templatePath));
		} catch (IOException e2) {
			e2.printStackTrace();
		}
        Template t=null;
        try {
            t = configuration.getTemplate(templateName+".ftl","utf-8"); //文件名
        } catch (IOException e) {
            e.printStackTrace();
        }
        String uuid = UUID.randomUUID().toString().replace("-", ""); //生成uuid
        String temp = generateFilePath + uuid + ".doc";
        File outFile = new File(generateFilePath);
		if (!outFile.exists()) {
			outFile.mkdirs();
		}
        Writer out = null;
        try {
            out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File(temp)),"UTF-8"));
        } catch (Exception e1) {
            e1.printStackTrace();
        }
        try {
            t.process(dataMap, out);
        } catch (TemplateException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
        	//关闭流
            try {
				out.flush();
				out.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
        }
        return temp;
    }

    /**
     * 导出文件下载
     * file：需要下载的文件
     */
    public void exportFile(HttpServletResponse response, File file) {
		OutputStream out = null;
		InputStream in = null;
		try {
			String filename = URLEncoder.encode(file.getName(), "UTF-8");
			response.setContentType("application/force-download");
			response.setHeader("Location", filename);
			response.setHeader("Content-Disposition", "attachment;filename="+ "OceanRegister.doc");
			out = response.getOutputStream();
			in = new FileInputStream(file.getPath());
			byte[] buffer = new byte[1024];
			int i = -1;
			while ((i = in.read(buffer)) != -1)
				out.write(buffer, 0, i);

		} catch (IOException e) {
		} finally {
			if (in != null)
				try {
					in.close();
				} catch (IOException e) {
				}
			if (out != null)
				try {
					out.close();
				} catch (IOException e) {
				}
			if (file.exists()) {
				file.delete();
			}
		}
	}

}
